const { update, remove, add, getCount, query } = require('../../utils/dbTools');
const tableName = 'patient';
const departmentTableName = 'department';
const staffTableName = 'staff';


/**
 * 获取数据总条数
 * 
*/

const patientTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取患者列表
 * 
*/
const patientList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    if (!page || !size) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    let sql = `select p.id,p.name,p.gender,p.birthday,p.phone,p.address,p.status,p.create_time,d.id as departmentid,d.name as department, s.id as doctorid, s.name as doctor, s.phone as doctorPhone`;
    sql += ` from patient p inner join ${departmentTableName} d on p.departmentid=d.id `;
    sql += ` inner join ${staffTableName} s on p.doctorid=s.id  `
    sql += ` order by p.id asc limit ${(page - 1) * size},${size} `;
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 新增患者信息
 * 
*/

const patientAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { name, departmentid, doctorid, gender, phone, birthday, address, status } = req.body;
    if (!name || !departmentid || !doctorid || !gender || !phone || !address || !status || !birthday) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    if (birthday) birthday = new Date(birthday).getTime();
    const data = {
        name, departmentid, doctorid, gender, phone, birthday, status, address, create_time: Date.now()
    }
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    res.send(output);
}

/**
 *  删除患者信息
 * 
*/

const patientRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }

    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 更新患者信息: 仅支持更新状态
*/
const patientUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, departmentid, doctorid, name, phone, gender, birthday, address, status } = req.body;
    if (!id || !name || !departmentid || !doctorid || !gender || !phone || !address || !status || !birthday) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    if (birthday) birthday = new Date(birthday).getTime();

    const result = await update(id, { departmentid, doctorid, name, phone, gender, birthday, address, status }, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}


/**
 * 搜索病人
 * 
 * 
*/
const patientSearch = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { keywords } = req.query;
    if (!keywords) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    let sql = `select id,name,phone from ${tableName} where name like '%${keywords}%' or phone like '%${keywords}%'`;
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}


module.exports = {
    patientAdd,
    patientRemove,
    patientUpdate,
    patientList,
    patientTotal,
    patientSearch
}